//
//  HTTPTool.m
//  Travel
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 0. All rights reserved.
//



#import "HTTPTool.h"
#import "AFNetworking.h"
@implementation HTTPTool
+(void)get:(NSString *)url
      body:(id)body
httpResult:(HTTPStyle)style
   success:(void(^)(id result))success
   failure:(void (^)(NSError *error))failure{
    
    //1获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    //判断返回的responseObject的数据类型
    switch (style) {
            //NSData类型
        case HTTP:
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            break;
            //JSON类型
         case JSON:
            break;
            //XML类型
        case XML:
             manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
//    [manager.requestSerializer setValue:@"sM4AOVdWfPE4DxkXGEs8VHz-jMoG0n4IsNSvO37QzhYCkHLwGXIpIvPnjF2cBGuR23nlXciLTIkZNWvETvfELA" forHTTPHeaderField:@"jsapi_ticket"];
//    [manager.requestSerializer setValue:@"wxe7a79cbb39084136" forHTTPHeaderField:@"appid"];
//    [manager.requestSerializer setValue:@"JeCPK7daCBFHcMx" forHTTPHeaderField:@"nonceStr"];
//    [manager.requestSerializer setValue:@"1435894222" forHTTPHeaderField:@"timestamp"];
    
    [manager.requestSerializer setValue:@"Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B466 @BreadTrip/5.1.1/zh-Hans Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"bt_devid=i_549d4f4fa7c35e63e2b086afadb0e7c7ca827f81fc62de7158a415472ba7dcf65ec66da7; Hm_lpvt_39ccc5353de4e1969b414e80e516d5a7=1435894222; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1435289251,1435540300,1435824769,1435884631; sessionid=f22ce74edcafac62c96a70f4a461fdce; btuid=49321ee80b5411e5b21ec81f66e3fe07; csrftoken=257b2dd844080beb570242cb0cb6330d" forHTTPHeaderField:@"Cookie"];
    
    //3让管理类调用get请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果成功返回数据
        if (success) {
             success(responseObject);
        }
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}


+(void)post:(NSString *)url
       body:(id)body
 httpResult:(HTTPStyle)style
requestBodyStyle:(RequestBodyStyle)bodyStyle
    success:(void(^)(id result))success
    failure:(void (^)(NSError *error))failure{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer=[AFHTTPRequestSerializer serializer];
            break;
        case BodyJSON:
            manager.requestSerializer=[AFJSONRequestSerializer serializer];
            break;
        default:
            break;
    }
    
    
    
    
    switch (style) {
        case HTTP:
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            break;
        case XML:
            manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
            
        default:
            break;
    }
    
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}
@end
